<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php


class CSmtpUser implements IUserSourceInterface
{
    private $_initialized = false;
    
    public function  __construct()
    {
    }


    public function getUser($userInfo){
        $userName = $userInfo['userName'];
        $password = $userInfo['password'];

        $option   = Option::model()->find("option_name='smtp_type'");
        $smtpType = $option["option_value"];

        if ($smtpType == "all"){
                        $index   = stripos($userName, "@");
            if ($index == 0){
                return false;
            }
            $suffix  = substr($userName, $index+1);
            if (empty($suffix)){
                return false;
            }

            $userLoginName = $userName;
            $host          = $this->getSmtp($suffix);
            $secure        = $this->getSmtpSecure($suffix, "all");
            $smtpPort      = $this->getSmtpPort($suffix, "all");
        } else {
            $option = Option::model()->find("option_name='smtp_server'");
            $smtpServer = $option["option_value"];
            if (empty($smtpServer)){
                return false;
            }
            $host = $smtpServer;
            $index   = stripos($userName, "@");
            if ($index == 0){
                $userLoginName = $userName."@".str_replace("smtp.","",$smtpServer);
            } else {
                $userLoginName = $userName;
            }

            $secure = $this->getSmtpSecure("", "custom");
            $smtpPort      = $this->getSmtpPort("", "custom");
        }

        try {
            $mailer = Yii::createComponent('application.extensions.mailer.EMailer');
            $mailer->Host = $host;
            $mailer->Port = $smtpPort;
            $mailer->SMTPSecure = strtolower($secure);
            $mailer->IsSMTP();
            $mailer->SMTPAuth = true;
            $mailer->Username = $userLoginName;
            $mailer->Password = $password;
            $ttt = $mailer->SmtpConnect();
        } catch (Exception $e) {
            $aa = $e->getMessage();
            return false;
        }

        $retData = array();
        $retData['user_name'] = $userLoginName;
        $retData['email']     = $userLoginName;
        return $retData;
    }

    
    public function judgeSelf(){
        return true;
    }


    
    public function getSmtpPort($suffix, $type){
        $smtpPort = 25;
        if ($type == "all"){
            if ($suffix=="gmail.com"){
                $smtpPort =  465;
            }
        } else {
            $optionPort   = Option::model()->find("option_name='smtp_port'");
            if (!empty($optionPort)){
                $smtpPort = $optionPort["option_value"];
            }
        }
        return $smtpPort;
    }

    
    public function getSmtpSecure($suffix, $type){
        $smtpSecure = "";
        if ($type == "all"){
            if ($suffix=="gmail.com"){
                $smtpSecure =  "ssl";
            }
        } else {
            $optionSecure   = Option::model()->find("option_name='smtp_secure'");
            if (!empty($optionSecure)){
                $smtpSecure = $optionSecure["option_value"];
                if (empty($smtpSecure)){
                    $smtpSecure = "";
                }
            }
        }
        return $smtpSecure;
    }

    
    public function getSmtp($suffix){
                if (strstr($suffix, "yahoo")){
            return "smtp.mail.".$suffix;
        }

                if ($suffix == "yimeier.net"){
            return "smtp.www.yimeier.net";
        } elseif ($suffix == "stedu.cn"){
            return "smtp.stedu.net";
        } elseif ($suffix == "pku.edu.cn"){
            return "water.pku.edu.cn";
        } elseif ($suffix == "email.com.cn"){
            return "www.email.com.cn";
        } elseif ($suffix == "ce.cn"){
            return "freemail.ce.cn";
        } else {
            return "smtp.".$suffix;
        }
    }
}
?>